<template>
  <!-- 配件综合情况查询 采购入库 -->
  <div>
    <FilterTable
      ref="table"
      columnsRef="reportPartInventoryssssabcd"
      :columns="columns"
      selectionType="single"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.selectColumns = arr)"
      :isExportExcel="true"
      :onCalcExportData="exportFun"
      exportFileName="配件综合情况查询-采购入库.xlsx"
      :dataSource="dataSource"
      :isMemoryPagination="true"
    >
      <!-- :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"-->
      <!-- 打印 -->
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-printer" :disabled="prindfBtnDisabled" :click="printHandle">打印</AjaxButton>
      </template>
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-printer" :click="exportHandle">导出</AjaxButton>
      </template>
    </FilterTable>
    <div v-if="printBtn">
      <BasePrint ref="print" :data="SalesprintList" template="templatepartInventory" />
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryPartInOutStockReport, getAllUser,exportPartInOutStockReport } from '@/api/part/report/billSearch';
import { dealerPrintApi } from '@/api/repair/index.js';
import { getStroageList } from '@/api/part/partOutbound/partBreak';
import { printData } from '@/api/part/partInventory/partInventory';
import { exportExcelFile } from '@/utils/index';
import moment from 'moment';
export default {
  mixins: [dictionary],
  props: {
    fetchParamObj: Object,
    type: String,
    userList: Array
  },

  data() {
    return {
      fetchApi: queryPartInOutStockReport,
      columns: this.createdTable(),
      fetchParams: { xhrAbort: true },
      selectColumns: [], // 选中行数据
      // userList: [], // 人员列表
      prindfBtnDisabled: false, // 打印
      printParams: {}, // 打印设置参数
      SalesprintList: {}, // 打印数据
      printDataList: [],
      printBtn: false,
      vDirectPrint: 0, // 直接打印 0=>false
      vPrintType: 99901001, // 打印机类型，针式
      vPageDirection: 99921002, // 走纸方向，纵向
      vPageType: 99911001, // 走纸方式,整张
      vBothPrint: 0, // 双面打印 false
      dataSource: []
    };
  },
  watch: {
    fetchParamObj(fetchParamObj) {
      // console.log(fetchParamObj, 'fetchParamObj----');
      this.initTableData(fetchParamObj);
    }
  },
  mounted() {
    this.getStorage();
    this.getEmployee();
    // this.getPrintParams();
  },
  methods: {
    exportFun(row) {
      for (const key in row) {
        switch (key) {
          // case 'FINISHED_DATE':
          //   // case 'FINISHED_DATE':
          //   // case 'BALANCE_TIME':
          //   // case 'SQUARE_DATE':
          //   if (row[key]) {
          //     row[key] = moment(row[key]).format('YYYY-MM-DD');
          //   }
          //   break;
          case 'CONSULTANT':
            if (row[key]) {
              row[key] = this.userList[row[key]];
            }
            break;
          case 'N_OLD_PURPIRCE_PRICE':
          case 'COST_PRICE':
          case 'COST_AMOUNT':
          case 'IN_AMOUNT_TAXED':
            if (row[key]) {
              row[key] = parseFloat(Number(row[key]).toFixed(2));
            }
            break;
          default:
            break;
        }
      }
    },
    // 打印数据接口
    async getPrintData() {
      const res = await printData(this.selectColumns[0].PART_BUY_ID); // 获取当前行数据
      this.SalesprintList = res.data;
    },
    async getPrintParams() {
      let params = { vModule: '90211009', vNormalCode: 'BJRK' };
      const res = await dealerPrintApi(params);
      if ((res.resultCode == 200) & res.data.length) {
        this.printParams = res.data[0];
      }

      console.log('打印的参数', this.printParams);
    },
    // 打印处理
    async printHandle() {
      // this.getPrintParams();
      if (this.selectColumns.length < 1) {
        return this.$notify.error({ title: '提示信息', message: '请选中一条数据' });
      }
      // 获取打印数据
      await this.getPrintData();
      this.printBtn = true;
      // this.SalesprintList = this.printdataList;
      // console.log(this.SalesprintList);

      this.SalesprintList.baseInfo.STOCK_IN_TYPE = '采购入库-' + this.createDictText(this.SalesprintList.baseInfo.STOCK_IN_TYPE, '8088');
      this.SalesprintList.baseInfo.USER_NAME = this.userList[this.SalesprintList.baseInfo.USER_ID];
      this.SalesprintList.baseInfo.FINISHED_TYPE = '入库时间';
      // 打印
      this.$nextTick(() => {
        this.$refs.print.EXCUTE_PRINT();
      });
    },
    async initTableData(obj) {
      const fetchParamObj = this.fetchParamObj;
      // this.fetchParams = Object.assign({}, fetchParamObj, { xhrAbort: false });
      this.fetchParams = Object.assign({}, fetchParamObj);
      try {
        this.$refs.table.START_LOADING();
        this.dataSource = [];
        let res = await queryPartInOutStockReport(this.fetchParams);
        this.dataSource = res.data;
        this.$refs.table.STOP_LOADING();
      } catch (error) {
        console.log(error);
      } finally {
        this.$refs.table.STOP_LOADING();
      }
    },
    //导出
    async exportHandle(){
      let res = await exportPartInOutStockReport(this.fetchParams);
      console.log(res,"导出::");
      try{
        exportExcelFile(res.data,'采购入库')
      }catch(e){
        console.log(e,"错误::");
      }
    },
    // 获取仓库
    async getStorage() {
      const res = await getStroageList();
      this.columns.find(x => x.dataIndex === 'STORAGE_CODE').dictItems = this.columns.find(x => x.dataIndex === 'STORAGE_CODE').editItems = res.data.map(x => ({
        text: x.STORAGE_NAME,
        value: x.STORAGE_CODE
      }));
    },
    // 操作员名称
    async getEmployee() {
      const res = await getAllUser();
      if (res.resultCode == 200) {
        res.data.map(e => {
          this.userList[e.userId] = e.employeeName;
        });
        this.columns.find(item => item.dataIndex == 'OPERATOR_NAME').editItems = this.columns.find(item => item.dataIndex == 'OPERATOR_NAME').dictItems = res.data.map(x => ({
          value: x.userId + '',
          text: x.employeeName
        }));
      } else {
        return this.$notity.error({ title: '提示信息', message: '获取人员信息失败' });
      }
      // this.columns.find(item => item.dataIndex === 'HANDLER').editItems = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
    },
    createdTable() {
      // 序号	客户代码 客户名称 入库单号 不含税总金额 货运单号 入帐日期 含税总金额 税额	税率 操作员名称 注册号码 备注 仓库
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '入库单号',
          dataIndex: 'STOCK_IN_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '原单编号',
        //   dataIndex: '',
        // },
        // {
        //   title: '对应单编号',
        //   dataIndex: 'CORRESPOND_ORDER',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '入库方式',
          dataIndex: 'STOCK_IN_TYPE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8088'),
          dictItems: this.createDictList('8088'),
          showOverflowTooltip: true
        },
        {
          title: '入库日期',
          dataIndex: 'FINISHED_DATE',
          // dateFormat: 'yyyy-MM-dd hh:mm:ss',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          showOverflowTooltip: true,
          exportFormat: {
            t: 'd'
          }
        },
        {
          title: '供应商代码',
          dataIndex: 'CUSTOMER_CODE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '供应商名称',
          dataIndex: 'CUSTOMER_NAME',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '仓库名称',
          dataIndex: 'STORAGE_CODE',
          sorter: true,
          editable: false,
          editType: 'select',
          editItems: [],
          dictItems: [],
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '税率',
        //   dataIndex: 'TAX',
        //   precision: 2,
        //   sorter: true,
        //   filter: true,
        //   filterType: 'number',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '操作员代码',
        //   dataIndex: 'UPDATED_BY',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '操作员名称',
          dataIndex: 'OPERATOR_NAME',
          sorter: true,
          // render: params => {
          //   return <span>{this.userList[params.row.OPERATOR_NAME]}</span>;
          // },
          editable: false,
          editType: 'select',
          editItems: [],
          dictItems: [],
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: '160',
          showOverflowTooltip: true
        },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '配件说明',
        //   dataIndex: 'PART_DESCRIPTION',
        //   sorter: true,
        //   showOverflowTooltip: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: 'ABC分类',
        //   dataIndex: 'ABC_TYPE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('8092'),
        //   dictItems: this.createDictList('8092'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.ABC_TYPE, '8092')}</span>;
        //   },
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '发票号',
        //   dataIndex: 'V_INVOICE_NO',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: 'CCC件类型',
        //   dataIndex: 'IS_CCC_PART',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '保质期',
        //   dataIndex: 'DWH_EXPIRED'
        // },
        {
          title: '发运单号',
          dataIndex: 'FYD_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '订货申请单号',
        //   dataIndex: 'V_R3_BILL_NO',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '发货中转库',
        //   dataIndex: 'TRANSFER_STOCK',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '计量单位',
        //   dataIndex: 'UNIT_NAME',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '入库数量',
          dataIndex: 'IN_QUANTITY',
          precision: 2,
          sorter: true,
          summation: true,
          filter: true,
          filterType: 'number',
          showOverflowTooltip: true
        },
        {
          title: '含税采购单价',
          dataIndex: 'N_OLD_PURPIRCE_PRICE',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number',
          showOverflowTooltip: true,
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '不含税成本单价',
          // dataIndex: 'COST_PRICE',
          dataIndex: 'IN_PRICE',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number',
          showOverflowTooltip: true
        },
        {
          title: '含税入库金额',
          dataIndex: 'IN_AMOUNT_TAXED',
          precision: 2,
          sorter: true,
          summation: true,
          filter: true,
          filterType: 'number',
          showOverflowTooltip: true,
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '不含税成本金额',
          // dataIndex: 'COST_AMOUNT',
          dataIndex: 'IN_AMOUNT',
          precision: 2,
          sorter: true,
          summation: true,
          filter: true,
          filterType: 'number',
          showOverflowTooltip: true,
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '库位',
          dataIndex: 'STORAGE_POSITION_CODE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        }
        // {
        //   title: '主组号',
        //   dataIndex: 'MAIN_GROUP_NO',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // }
        // {
        //   title: '备注',
        //   dataIndex: 'REMARK',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '发布单号',
        //   dataIndex: 'fake1',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '销售经销商代码',
        //   dataIndex: 'fake2',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // }
        // {
        //   title: '不含税总金额',
        //   dataIndex: 'IN_AMOUNT',
        //   sorter: true
        // },
        // {
        //   title: '货运单号',
        //   dataIndex: 'DELVIERY_NO',
        //   sorter: true
        // },
        // {
        //   title: '入帐日期',
        //   dataIndex: 'FINISHED_DATE',
        //   sorter: true,
        //   width: '160'
        // },
        // {
        //   title: '含税总金额',
        //   dataIndex: 'IN_AMOUNT_TAXED',
        //   sorter: true
        // },
        // {
        //   title: '税额',
        //   dataIndex: 'TAX_AMOUNT',
        //   sorter: true
        // },
        // {
        //   title: '注册号码',
        //   dataIndex: 'ORDER_REGEDIT_NO',
        //   sorter: true
        // },
        // {
        //   title: '备注',
        //   dataIndex: 'REMARK',
        //   sorter: true
        // }
      ];
    }
  }
};
</script>

<style lang="less" scoped>
/* .form-group {
  margin-top: 10px;
  span {
    height: 50px;
    line-height: 50px;
    text-align: right;
    width: 250px;
    padding-left: 100px;
  }
} */
</style>
